<?php
/**
 * Created by PhpStorm.
 * User: mac
 * Date: 19/9/26
 * Time: 14:27
 */

namespace app\store\controller;

use app\common\bean\GlobalBean;
use app\common\bean\ListMap;
use app\common\bean\StoreUserBean;
use app\common\controller\StoreController;
use app\common\enum\StoreUserIsUpdatePwEnum;
use app\common\enum\StoreUserStatusEnum;
use app\common\manage\StoreUserManage;
use app\common\manage\StoreRoleManage;
use app\common\utils\ExitJsonUtil;

class StoreUser extends StoreController
{
    /**
     * @return mixed
     * @throws \Exception
     */
    public function userList(){
        $listMap = new ListMap();
        $listMap->setData($this->getParams);
        $userMap = StoreUserManage::getInstance()->getPaginatorList($listMap);
        $storeRoleSelect = StoreRoleManage::getInstance()->getStoreRoleSelect($this->postParams[GlobalBean::$storeId]);
        $this->assign("roleList",$userMap->getList());
        $this->assign("currentPage",$userMap->getCurrentPage());
        $this->assign("totalCount",$userMap->getTotalCount());
        $this->assign('storeRoleSelect',$storeRoleSelect);
        $this->assign('storeStatusSelect',StoreUserStatusEnum::getArrayName());
        return $this->fetch();
    }

    /**
     * 添加管理员
     * @throws \Exception
     */
    public function addUser(){
        if(request()->isPost()){
            $storeUserBean = new StoreUserBean();
            $storeUserBean->setData($this->postParams);
            $result = StoreUserManage::getInstance()->add($storeUserBean);
            ExitJsonUtil::getInstance()->exitData($result);
        }
        $storeRoleSelect = StoreRoleManage::getInstance()->getStoreRoleSelect($this->postParams[GlobalBean::$storeId]);
        $this->assign('storeRoleSelect',$storeRoleSelect);
        $this->assign('storeIsUpPassSelect',StoreUserIsUpdatePwEnum::getArrayName());
        $this->assign('storeStatusSelect',StoreUserStatusEnum::getArrayName());
        return $this->fetch();
    }

    /**
     * 修改管理员
     * @throws \Exception
     */
    public function editUser(){
        $storeUserBean = new StoreUserBean();
        $storeUserBean->setData($this->getParams);
        if(request()->isPost()){
            $storeUserBean->setData($this->postParams);
            $result = StoreUserManage::getInstance()->edit($storeUserBean);
            ExitJsonUtil::getInstance()->exitData($result);
        }
        $storeUserBean = StoreUserManage::getInstance()->getInfo($storeUserBean);
        $storeRoleSelect = StoreRoleManage::getInstance()->getStoreRoleSelect($this->postParams[GlobalBean::$storeId]);
        $this->assign('self',false);
        $this->assign('storeRoleSelect',$storeRoleSelect);
        $this->assign('storeIsUpPassSelect',StoreUserIsUpdatePwEnum::getArrayName());
        $this->assign('storeStatusSelect',StoreUserStatusEnum::getArrayName());
        $this->assign('storeUser',$storeUserBean->getData());
        return $this->fetch();
    }

    /**
     * 删除管理员
     * @throws \Exception
     */
    public function delUser(){
        $storeUserBean = new StoreUserBean();
        $storeUserBean->setData($this->getParams);
        $result = StoreUserManage::getInstance()->delete($storeUserBean);
        ExitJsonUtil::getInstance()->exitData($result);
    }
}